package com.example.onion.hxchat.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.onion.hxchat.util.EmailUtil;
import com.example.superyi.myteam.R;

import java.util.Random;

/**
 * @author:onion
 */
public class AlterPassWordActivity extends AppCompatActivity {

    private EditText alterUserName;//修改用户的id
    private EditText alterNewPassword;//要修改的密码
    private EditText alterNewPasswordAgain;//确认修改的密码（二次输入)
    private EditText yanZhengCode;//确认修改的密码（二次输入)
    private Button submitBtn; // 提交修改申请的按钮
    private Button sendCodeBtn; // 提交修改申请的按钮
    private String code;//验证码

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alter_password);
        initView();
    }

    private void initView()
    {
        alterUserName = findViewById(R.id.alter_password_username_et);
        alterNewPassword = findViewById(R.id.alter_newpassword_et);
        alterNewPasswordAgain = findViewById(R.id.alter_newpassword_again_et);
        yanZhengCode = findViewById(R.id.alter_password_code_et);
        submitBtn = findViewById(R.id.alter_password_submit_btn);
        sendCodeBtn = findViewById(R.id.alter_password_send_code_btn);
        sendCodeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //在用户输入好用户id后点击向邮箱发送验证码
                code = getFourRandom();
                EmailUtil.sendMail("1246777082@qq.com",code);
            }
        });
        submitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(AlterPassWordActivity.this,"发送充值密码请求",Toast.LENGTH_SHORT).show();

                String newPassword = alterNewPassword.getText().toString().trim();
                String newPasswordAgain = alterNewPasswordAgain.getText().toString().trim();
                String codeInput = yanZhengCode.getText().toString().trim();
                if (newPassword.equals(newPasswordAgain)&& code.equals(codeInput)){
                    Toast.makeText(AlterPassWordActivity.this,"发送重置密码请求",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
    public static String getFourRandom(){
        Random random = new Random();
        StringBuilder fourRandom = new StringBuilder(random.nextInt(10000) + "");
        int randLength = fourRandom.length();
        if(randLength<4){
            for(int i=1; i<=4-randLength; i++)
                fourRandom.insert(0, "0");
        }
        return fourRandom.toString();
    }
}
